var node, conId='';

function renderClickEvent() {
    $("[click-event]").each(function () {
        currentElement = $(this);
        var clickEvent = currentElement.attr("click-event");
        if (clickEvent == null || clickEvent == "") return true;
        switch (clickEvent) {
            case "1":
                $(this).on("click", function (data) {
                    testClick();
                });
                break
            case "2":
                $(this).on("click", function (data) {
                    selectUser(this);
                });
                break
            case "3":
                $(this).on("click", function (data) {
                    selectOrg(this);
                });
                break
            case "4":
                $(this).on("change", function (data) {
                    var chinese = $(this).val();
                    $.post(`/pinyin/getPinYinHeadChar?chinese=${chinese}`,(data)=>{
                        $("[default-value]").each(function () {
                            var defaultValue = $(this).attr("default-value");
                            if(defaultValue === "4"){
                                $(this).val(data);
                            }
                        });
                    });
                });
                break
            case "5":
                $(this).on("click", function (data) {
                    selectCustomer(this);
                });
                break
            case "6":
                $(this).on("click", function (data) {
                    selectContract(this);
                });
                break
            case "7":
                $(this).on("click", function (data) {
                    selectProduct(this);
                });
                break
            case "8":
                $(this).on("click", function (data) {
                    selectProject(this);
                });
                break

            case "9":
                $(this).on("click", function (data) {
                    selectArea(this);
                });
                break

            case "10":
                $(this).on("click", function (data) {
                    chooseProduct(this);
                });
                break

            case "13":
                $(this).on("click", function (data) {
                    selectDeputy(this);
                });
                break

            case "14":
                $(this).on("click", function (data) {
                    numberCase(this);
                });
                break

        }
    });
}

//金额格式化（保留两位小数）
function numberCase(e) {
    var num=$(e).val();
    var num1 = new Number(num);
    $(e).val(num1.toFixed(2));

}
//产品分类
function chooseProduct(e) {
    node = $(e);
    window.frameElement.id = "area";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择产品分类'
        , area: ['500px', '720px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/chooseProduct.html'
        , zIndex: layer.zIndex,

        end: function () {
            var customerArea = sessionStorage.getItem('erp_tmt_product');
            area = JSON.parse(customerArea);
            if ("" != area.pbcName && null != area.pbcName) {
                node.val(area.pbcName);
                node.attr("value-code", area.pbcId)
                sessionStorage.removeItem("erp_tmt_product")
            } else {
                layer.msg('请重新选择分类', {
                    icon: 5,
                    time: 2000,
                });
            }
        }
    });
}

//选择区域
function selectArea(e) {
    node = $(e);
    window.frameElement.id = "area";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择客户区域'
        , area: ['500px', '720px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/selectArea.html'
        , zIndex: layer.zIndex,

        end: function () {
            var customerArea = sessionStorage.getItem('erp_yy_customer_area');
            area = JSON.parse(customerArea);
            if ("" != area.srgName && null != area.srgName) {
                node.val(area.srgName);
                node.attr("value-code", area.id)
                sessionStorage.removeItem("erp_yy_customer_area")
            } else {
                layer.msg('请重新选择区域', {
                    icon: 5,
                    time: 2000,
                });
            }
        }

    });
}

//选择人员
function selectUser(e) {
    // console.log($(e));
    node = $(e);
    // layer.alert("选择人员");
    window.frameElement.id = "user";
    var ssname = "erp_next_node_executor"
    var frameId = window.frameElement && window.frameElement.id || '';

    sessionStorage.setItem(ssname, node.attr("value-code"))
    parent.layer.open({
        type: 2
        , title: '选择人员'
        , area: ['900px', '650px']
        , maxmin: false
        // ,content: '/base/workFlow/workFlow/selectUser.html?iframeId='+frameId
        , content: '/base/workFlow/workFlow/selectUser.html?sessionStrogeName=' + ssname
        , zIndex: layer.zIndex
        , end: function () {
            var temp = sessionStorage.getItem(ssname)
            var resData

            if (temp) {
                resData = JSON.parse(temp)
            } else return
            if (resData.optype == "ok") {
                var strTemp = ""
                resData.objs.forEach(function (item) {
                    strTemp += item.name + " "
                })
                // console.log(item)
                node.val(strTemp)

                var strTemp2 = JSON.stringify(resData.ids)
                strTemp2 = strTemp2.replace("[", "");
                strTemp2 = strTemp2.replace("]", "");
                node.attr("value-code", strTemp2)
            }

            sessionStorage.setItem(ssname, "")
        }
    });
}

//选择单位部门
function selectOrg(e) {
    node = $(e);
    window.frameElement.id = "org";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择单位部门'
        , area: ['400px', '700px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/selectOrg.html?iframeId=' + frameId
        , zIndex: layer.zIndex

    });
}

//选择客户
function selectCustomer(e) {
    node = $(e);
    window.frameElement.id = "customer";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择客户'
        , area: ['800px', '700px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/select.html?iframeId=' + frameId + "&type=customer"
        , zIndex: layer.zIndex
        , end: function () {
            var json = node.attr("value-json");
            var customer
            if (json) {
                customer = JSON.parse(json.replace(/\'/g, '\"'))
            } else {
                return
            }
            $.ajax({
                async: false,
                type: "post",
                url: "/erp-customer-contact/selectAll",
                data: {"clientId": customer.id},
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                timeout: 60000,//超时时间设置为1分钟；
                success: function (data) {
                    if (data.code === 200 ) {
                        conId = customer.id;
                        $("[click-event]").each(function () {
                            currentElement = $(this);
                            var clickEvent = currentElement.attr("click-event");
                            if (clickEvent == null || clickEvent == "") return true;
                            switch (clickEvent) {
                                case "13":
                                    $(this).attr('value-code',"");
                                    $(this).attr('value',"");
                                    $(this).val("");
                                    break

                            }
                        });
                    }
                },
                error: function (a, b, c) {
                    // 请求出错处理,依然初始化咨询节点信息
                    if (b == 'timeout') {
                        layer.alert("请求超时，请再次尝试！");
                    } else {
                        layer.alert(url + "请求异常");
                    }
                }
            });
            node.val(customer.clientNameCn);

            form.render();
        }
    });
}

//选择对方代表
function selectDeputy(e) {
    if(conId==''){
        layer.alert("请先选择客户！");
        return true;
    }
    node = $(e);
    window.frameElement.id = "customer";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择客户'
        , area: ['1000px', '700px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/selectContracts.html?iframeId=' + frameId + "&id="+conId
        , zIndex: layer.zIndex
        , end: function () {
            var json = node.attr("value-json");
            var contract = JSON.parse(json.replace(/\'/g, '\"'));
            node.val(contract.contacts);
            // $("[name='other']").val(contract.id);
        }
    });
}

//选择合同
function selectContract(e) {
    var ecid = $("[name='eoCustomer']").attr("value-code");
    console.log(ecid);
    node = $(e);
    window.frameElement.id = "contract";
    var frameId = window.frameElement && window.frameElement.id || '';
    if(ecid == null || ecid == ""){
        layer.msg('请选择客户！');
    }else{
        parent.layer.open({
            type: 2
            , title: '选择合同'
            , area: ['800px', '700px']
            , maxmin: false
            , content: '/base/workFlow/workFlow/select.html?iframeId=' + frameId + "&type=contract&fid="+ecid
            , zIndex: layer.zIndex
            , end: function () {
                var json = node.attr("value-json");
                var contract = JSON.parse(json.replace(/\'/g, '\"'));
                node.val(contract.ecName);
                $("[name='contractCode']").val(contract.ecNumber);
            }
        });
    }
}

//选择产品
function selectProduct(e) {
    node = $(e);
    window.frameElement.id = "product";
    var frameId = window.frameElement && window.frameElement.id || '';
    parent.layer.open({
        type: 2
        , title: '选择产品'
        , area: ['800px', '700px']
        , maxmin: false
        , content: '/base/workFlow/workFlow/select.html?iframeId=' + frameId + "&type=product"
        , zIndex: layer.zIndex
        , end: function () {
            var json = node.attr("value-json");
            var product = JSON.parse(json.replace(/\'/g, '\"'));
            node.val(product.proName);
            $("[name='model']").val(product.model);
            loadSelect("units", 77, product.units);
        }
    });
}

//选择项目
function selectProject(e) {
    // node = $(e);
    // var frameId = window.frameElement && window.frameElement.id || '';
    // parent.layer.open({
    //     type: 2
    //     ,title: '选择项目'
    //     ,area: ['800px', '700px']
    //     ,maxmin: false
    //     ,content: '/base/workFlow/workFlow/select.html?iframeId='+frameId
    //     ,zIndex: layer.zIndex
    // });
}


// layui 绑定事件
layui.use(['form'], function () {
    form = layui.form;

    //单选框监听-产品添加-可调范围
    form.on('radio(callableScope)', function (data) {
        console.log("ttttttttttttttttttt"); //得到radio原始DOM对象
        console.log(data.elem); //得到radio原始DOM对象
        console.log(data.value); //被点击的radio的value值
    });

});

//加载下拉框
function loadSelect(id, val, d) {
    var sel = $("[name='" + id + "']");
    sel.html("<option value=''></option>")
    $.ajax({
        async: false,
        type: "post",
        url: "/wf/dictionary/list",
        data: {"parentId": val},
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        timeout: 60000,//超时时间设置为1分钟；
        success: function (data, textStatus) {
            if (data.code == "200") {
                $.each(data.rows, function (index, value) {
                    if (value.value == d) {
                        sel.append("<option value='" + value.value + "' selected>" + value.cnName + "</option>");
                    } else {
                        sel.append("<option value='" + value.value + "'>" + value.cnName + "</option>");
                    }
                });
            }
        },
        error: function (a, b, c) {
            // 请求出错处理,依然初始化咨询节点信息
            if (b == 'timeout') {
                layer.alert("请求超时，请再次尝试！");
            } else {
                layer.alert(url + "请求异常");
            }
        }
    });

    form.render();
}